//
//  UIButton+layout.m
//  SmartCommunity
//
//  Created by 崔国冠 on 2017/9/18.
//  Copyright © 2017年 崔国冠. All rights reserved.
//

#import "UIButton+layout.h"
#import "NSString+Util.h"

@implementation UIButton (layout)
#pragma mark 图片文字布局
- (void)setButtonImageTitleStyle:(ButtonImageTitleStyle)style
                            spacing:(CGFloat)spacing {
    CGFloat imageWidth = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    
    CGFloat labelWidth = [self.titleLabel.text widthForFont:self.titleLabel.font];
    CGFloat labelHeight = [self.titleLabel.text heightForFont:self.titleLabel.font
                                                           width:labelWidth];
    
    //  image中心移动的X距离
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;
    //  image中心移动的Y距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;
    
    //  label中心移动的X距离
    CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;
    //  label中心移动的Y距离
    CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;
    
    CGFloat tempWidth = MAX(labelWidth, imageWidth);
    CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
    
    CGFloat tempHeight = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
    
    switch (style) {
        case ButtonImageTitleStyleLeft: {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, - spacing / 2, 0, spacing / 2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing / 2, 0, - spacing / 2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing / 2, 0, spacing / 2);
        }
            break;
        case ButtonImageTitleStyleRight: {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing / 2, 0, - (labelWidth + spacing / 2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - (imageWidth + spacing / 2), 0, imageWidth + spacing / 2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing / 2, 0, spacing / 2);
        }
            break;
        case ButtonImageTitleStyleTop: {
            self.imageEdgeInsets = UIEdgeInsetsMake(- imageOffsetY, imageOffsetX, imageOffsetY, - imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, - labelOffsetX, - labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, - changedWidth / 2, changedHeight - imageOffsetY, - changedWidth / 2);
        }
            break;
        case ButtonImageTitleStyleBottom: {
            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, - imageOffsetY, - imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(- labelOffsetY, - labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight - imageOffsetY, - changedWidth / 2, imageOffsetY, - changedWidth / 2);
        }
            break;
        default: {
            
        }
            break;
    }
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent* )event
{
    CGRect bounds = self.bounds;
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(54.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}

- (void)setHighlighted:(BOOL)highlighted
{
    
}
@end
